package com.aysafety.eventmodel.corn;

import com.aysafety.eventmodel.common.DateUtils;
import com.aysafety.eventmodel.service.geosound.GeosoundService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: iotDataPlatform
 * @description: 地音定时任务
 * @author: ChengQi
 * @create: 2019-02-18 11:01
 **/
@Component
@Order(3)
public class GeosoundTask {

    @Autowired GeosoundService geosoundService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * @Description: 能量平均值 、能量偏差值
     * @Param: []
     * @return: void
     * @Author: ChengQi
     * @Date: 2019/2/18
     */
    //    @Scheduled(cron = "0 0/1 * * * ? ")
    public void emergoaAvgTask() {
        logger.info("正在计算能量平均值 、能量偏差值...");
        String currentTime = DateUtils.getTime();
        try {

            geosoundService.calAvg(currentTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

        logger.info("完成能量平均值 、能量偏差值计算...currentTime:" + currentTime);
    }

    //    @Scheduled(cron = "0 40 23 12 * ? ")
    //@Scheduled(cron = "0 04 16 * * ?")
    public void emergoaAvgHistoryTask() {
        logger.info("正在计算能量平均值 、能量偏差值...");
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-04-20 00:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //开始时间必须小于结束时间
                Date beginDate = dateFormat1.parse("2018-08-27 00:00:00");
                Date endDate = dateFormat1.parse("2018-09-01 00:00:00");
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    geosoundService.calAvg(currentTime);
                    logger.info("完成能量平均值 、能量偏差值计算...currentTime:" + currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 1); // 日期加1天
                    date = c.getTime();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @Description: 脉冲、脉冲因子、脉冲偏差值
     * @Param: []
     * @return: void
     * @Author: ChengQi
     * @Date: 2019/2/18
     */
    //    @Scheduled(cron = "0 0/1 * * * ? ")
    public void calpulse() {
        logger.info("正在计算脉冲、脉冲因子、脉冲偏差值...");
        String currentTime = DateUtils.getTime();
        try {
            geosoundService.calpulse(currentTime);

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("完成脉冲、脉冲因子、脉冲偏差值计算...currentTime:" + currentTime);
    }

    //    @Scheduled(cron = "0 40 23 12 * ? ")
    //@Scheduled(cron = "0 04 16 * * ?")
    public void calpulseHistory() {
        logger.info("正在计算脉冲、脉冲因子、脉冲偏差值...");
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-04-20 00:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //开始时间必须小于结束时间
                Date beginDate = dateFormat1.parse("2018-08-27 00:00:00");
                Date endDate = dateFormat1.parse("2018-09-01 00:00:00");
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    geosoundService.calpulse(currentTime);
                    logger.info("完成脉冲、脉冲因子、脉冲偏差值计算...currentTime:" + currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 1); // 日期加1天
                    date = c.getTime();
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

    }

    /**
     * @Description: 每日能量偏差高值总数Ds、每日能量偏差高值总数Ds、日总、日均、日最高
     * @Param: []
     * @return: void
     * @Author: ChengQi
     * @Date: 2019/2/19
     */
    //    @Scheduled(cron = "0 0 2 1/1 * ? ")
    public void daily() {
        logger.info("开始计算每日能量偏差高值总数Ds、每日能量偏差高值总数Ds、日总、日均、日最高");
        String currentTime = DateUtils.getTime();
        try {
            geosoundService.daily(currentTime);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("完成每日能量偏差高值总数Ds、每日能量偏差高值总数Ds、日总、日均、日最高计算");
    }


    /**
     * @Description: 每日能量偏差高值总数Ds、每日能量偏差高值总数Ds、日总、日均、日最高
     * @Param: []
     * @return: void
     * @Author: ChengQi
     * @Date: 2019/2/19
     */
    //    @Scheduled(cron = "0 40 23 12 * ? ")
    public void dailyHistory() {
        logger.info("开始计算每日能量偏差高值总数Ds、每日能量偏差高值总数Ds、日总、日均、日最高");
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-03-14 00:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //开始时间必须小于结束时间
                Date beginDate = dateFormat1.parse("2019-02-02 00:00:00");
                Date endDate = dateFormat1.parse("2019-03-14 00:00:00");
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    geosoundService.daily(currentTime);
                    c.setTime(date);
                    c.add(Calendar.DATE, 1); // 日期加1天
                    date = c.getTime();
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("完成每日能量偏差高值总数Ds、每日能量偏差高值总数Ds、日总、日均、日最高计算");
    }

    /**
     * @Description: 地音-综合预警
     * @Param: []
     * @return: void
     * @Author: ChengQi
     * @Date: 2019/2/28
     */
    //    @Scheduled(cron = "0 0 0/1 * * ? ")
    /*public void dailyComprehensiveWarning() throws Throwable {

        String currentTime = DateUtils.getTime();
        logger.info("正在计算地音综合预警currentTime..." + currentTime);
        try {
            geosoundService.dailyComprehensiveWarning(currentTime);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        logger.info("完成地音综合预警计算currentTime..." + currentTime);
    }*/

    /**
     * @Description: 地音-综合预警
     * @Param: []
     * @return: void
     * @Author: ChengQi
     * @Date: 2019/2/28
     */
    //@Scheduled(cron = "0 35 15 28 2 *")
    //@Scheduled(cron = "30 01 17 13 * ?")
    public void dailyComprehensiveWarningHistory() throws Throwable {

        logger.info("正在计算地音综合预警...");

        try {
            Calendar c = Calendar.getInstance();
            DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //开始时间必须小于结束时间
            Date beginDate = dateFormat1.parse("2018-08-27 11:46:42");
            Date endDate = dateFormat1.parse("2019-04-7 17:00:00");
            Date date = beginDate;
            while (!date.equals(endDate)) {
                String currentTime = DateUtils.getTime(date);
                geosoundService.warningV2(currentTime);
                c.setTime(date);
                c.add(Calendar.HOUR_OF_DAY, 1); // 日期加1天
                date = c.getTime();
            }
            System.out.println("地音综合预警计算结束。。。");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("完成地音综合预警计算...");
    }

    /** 
    * @Description: 综合预警计算实时W值
    * @Param: [] 
    * @return: void 
    * @Author: ChengQi 
    * @Date: 2019/4/3 
    */
    //@Scheduled(cron = "0 01 16 * * ?")
    public void warning() {
        String currentTime = DateUtils.getTime();
        logger.info("正在计算地音综合预警WcurrentTime..." + currentTime);
        try {
            geosoundService.warning(currentTime);
        } catch (Throwable throwable) {
            logger.error("frequency error,currentTime:" + currentTime);
        }
        logger.info("完成地音综合预警W计算currentTime..." + currentTime);
    }


    /**
     * 计算历史数据的脉冲、累计能量和平均能量
     */
    @Scheduled(cron = "30 33 09 14 * ?")
    public void computePulseAndEnergyHistoryTask () {
        logger.info("正在计算中......");
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-05-15 00:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //开始时间必须小于结束时间
                Date beginDate = dateFormat1.parse("2018-09-02 12:05:43");
                Date endDate = dateFormat1.parse("2019-4-01 12:45:36");
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    geosoundService.computePulseAndEnergy(currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 10); // 日期加10分钟
                    date = c.getTime();
                    logger.info("正在计算中......");
                }
            }
        } catch (Throwable throwable) {
            logger.info("脉冲、累计能量和平均能量计算失败...", throwable);
        }
        logger.info("完成脉冲、累计能量和平均能量...");
    }


    /**
     * 计算实时数据的脉冲、累计能量和平均能量
     */
    public void computePulseAndEnergyTask () {

        String currentTime = DateUtils.getTime();
        logger.info("正在计算中..." + currentTime);
        try {
            geosoundService.computePulseAndEnergy(currentTime);
        } catch (Throwable throwable) {
            logger.info("脉冲、累计能量和平均能量计算失败...", throwable);
        }
        logger.info("完成脉冲、累计能量和平均能量...");
    }
}
